import { findPermissionTreeByUserId, findPermissionTree } from "@/api/permission/permission.js"
import { flatPermissionTreeToArray } from '@/util/util.js'

export default {
    async setUserPermissions({ commit, state }) {
        let userId = sessionStorage.getItem("userId");
        const r = await findPermissionTreeByUserId(userId);
        let userPermissionsTree = r.data.children;
        commit('setUserPermissionsTree', userPermissionsTree);

        let flatPermissions = [];
        flatPermissionTreeToArray(userPermissionsTree, flatPermissions);
        
        let userStringPermissions = flatPermissions.map(p => p.permissionExpr);
        
        commit('setUserStringPermissions', userStringPermissions);
    }
}